package com.mlink.ai.chat.utils

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.webkit.MimeTypeMap
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import java.util.Locale


object MediaUtils {

    fun getMimeType(uri: Uri, context: Context): String {
        var mimeType: String? = null
        mimeType = if ("content" == uri.scheme) {
            val contentResolver: ContentResolver = context.contentResolver
            contentResolver.getType(uri)
        } else {
            val fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri.toString())
            MimeTypeMap.getSingleton()
                .getMimeTypeFromExtension(fileExtension.lowercase(Locale.getDefault()))
        }
        return mimeType ?: ""
    }

    fun setVideoThumbnailToImageView(context: Context, imageView: ImageView, videoUri: Uri) {
        Glide.with(context)
            .setDefaultRequestOptions(RequestOptions().frame(100 * 1000))
            .load(videoUri)
            .into(imageView)
    }
}